package com.cloud.code.算法.链表;

public class LinkedList<E> {

    private int size;

    private Node dummyHead;

    public LinkedList() {
        dummyHead = new Node(null, null);
        size = 0;
    }


    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void addFrist(E e) {
        add(0, e);
    }

    public void add(int index, E e) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("illegal  index");

        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }

        prev.next = new Node(e, prev.next);
        size++;
    }


    public void addList(E e) {
        add(size, e);
    }

    public void set(int index, E e) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("illegal  index");

        Node cur = dummyHead.next;
        for (int i = 0; i < size; i++) {
            cur = cur.next;
        }

        cur.data = e;
    }

    public boolean contains(E e) {
        Node cur = dummyHead.next;
        if (e == null) {
            while (cur != null) {
                if (cur.data == null) {
                    return true;
                }
                cur = cur.next;
            }
        } else {
            while (cur != null) {
                if (e.equals(cur.data)) {
                    return true;
                }
                cur = cur.next;
            }
        }

        return false;
    }


    public E get(int index) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("illegal  index");

        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }

        return (E) cur;
    }

    public E remove(int index){
        if (index < 0 || index > size)
            throw new IllegalArgumentException("illegal  index");

        Node pre = dummyHead;
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }
        Node retNode = pre.next;
        pre.next = retNode.next;
        retNode.next = null;
        size--;
        return (E) retNode.data;
    }

    public E removeFast(){

        return remove(0);
    }

    public E getFirst() {
        return get(0);
    }

    public E getLast() {
        return get(size - 1);
    }

    @Override
    public String toString() {

        StringBuffer stringBuffer = new StringBuffer();
        Node cur = dummyHead.next;
        while (cur != null) {
            stringBuffer.append(cur + "-->");
            cur = cur.next;
        }
        return stringBuffer.toString();
    }

    public class Node<E> {

        public Node next;

        public E data;

        public Node(E data, Node next) {
            this.next = next;
            this.data = data;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {

            return data+"";
        }
    }
}
